﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.DataModel;
using QLBH.Model.Controller;
using System.Windows.Forms;
using System.ComponentModel;
using QLBH.StandardUI;
using QLBH.Category;
using CrystalDecisions.CrystalReports.Engine;
using QLBH.Reports;
using System.Data;
using QLBH.ModelInterface;

namespace QLBH.SaleProduct
{
    public class PhieuNhapHangView : FormViewBase, IReportable
    {
        private IPhieuNhapKhoCTModel model;

        public IPhieuNhapKhoCTModel Model
        {
            get { return model; }
            set 
            {
                model = value; 
            }
        }
        private eDlgType dlgType;

        public eDlgType DlgType
        {
            get { return dlgType; }
            set 
            { 
                dlgType = value; 
                if (dlgType==eDlgType.ADD)
                {
                    model = ModelFactory.ModelFactory.GetPhieuNhapKhoCTModelInstance();
                }
            }
        }
        private KHO khoHang;
        private ListView listViewPopup;
        private List<MATHANG> mathangs;
        private System.ComponentModel.IContainer components;
        private MATHANG selected;
        private eDlgType functionType = eDlgType.ADD;
        private DataGridViewTextBoxColumn pNKIDDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn mHIDDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn mHCODEDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn tENMHDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn dVTDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn sOLUONGDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn dONGIADataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn cHIETKHAUDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn tHANHTIENDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn gHICHUDataGridViewTextBoxColumn;
        private ToolStripButton btnIn;
        private BindingSource nKMatHangModelBindingSource;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn6;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn7;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn8;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn9;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn10;
        private INKMatHangModel editItem;
        public PhieuNhapHangView()
            :base()
        {
            DlgType = eDlgType.ADD;
            InitializeComponent();
        }
#region controls
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label lbNgayTao;
        private System.Windows.Forms.Label lbNguoiTao;
        private System.Windows.Forms.Label lbAddKho;
        private System.Windows.Forms.Label lbSoPhieu;
        private System.Windows.Forms.ComboBox cmbTenKho;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.TextBox tbxSoLuong;
        private System.Windows.Forms.Button btnDelete;
        private System.Windows.Forms.Button btnAdd;
        private StandardUI.AutoCompleteTextBox tbxMatHangSeach;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.RadioButton radioButton1;
        private StandardUI.DataGridViewEx dataGridView1;
        private System.Windows.Forms.ToolStripButton btnAddPNK;
        private System.Windows.Forms.ToolStripButton btnSavePNK;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private System.Windows.Forms.ToolStripButton btnPrintPreview;
        private System.Windows.Forms.ToolStripButton btnPrint;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private System.Windows.Forms.ToolStripButton btnFindPNK;
        private System.Windows.Forms.Label lbTongTienThanhToan;
        private System.Windows.Forms.Label lbTongTienHang;
        private System.Windows.Forms.Label label13;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox tbxThue;
        private System.Windows.Forms.TextBox tbxChietKhau;
        private System.Windows.Forms.GroupBox groupBox2;
    
#endregion
        
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PhieuNhapHangView));
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.btnAddPNK = new System.Windows.Forms.ToolStripButton();
            this.btnSavePNK = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnPrintPreview = new System.Windows.Forms.ToolStripButton();
            this.btnIn = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.btnFindPNK = new System.Windows.Forms.ToolStripButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.tbxChietKhau = new System.Windows.Forms.TextBox();
            this.tbxThue = new System.Windows.Forms.TextBox();
            this.lbNgayTao = new System.Windows.Forms.Label();
            this.lbNguoiTao = new System.Windows.Forms.Label();
            this.lbAddKho = new System.Windows.Forms.Label();
            this.lbTongTienThanhToan = new System.Windows.Forms.Label();
            this.lbTongTienHang = new System.Windows.Forms.Label();
            this.lbSoPhieu = new System.Windows.Forms.Label();
            this.cmbTenKho = new System.Windows.Forms.ComboBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label13 = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.dataGridView1 = new QLBH.StandardUI.DataGridViewEx();
            this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn7 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn8 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn9 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn10 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nKMatHangModelBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.tbxSoLuong = new System.Windows.Forms.TextBox();
            this.btnDelete = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.tbxMatHangSeach = new QLBH.StandardUI.AutoCompleteTextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.tableLayoutPanel1.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.nKMatHangModelBindingSource)).BeginInit();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 1;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.toolStrip1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.groupBox1, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.groupBox2, 0, 2);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 3;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(0, 0);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // toolStrip1
            // 
            this.toolStrip1.BackColor = System.Drawing.SystemColors.ButtonFace;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnAddPNK,
            this.btnSavePNK,
            this.toolStripSeparator1,
            this.btnPrintPreview,
            this.btnIn,
            this.toolStripSeparator2,
            this.btnFindPNK});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(1, 25);
            this.toolStrip1.TabIndex = 2;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // btnAddPNK
            // 
            this.btnAddPNK.Image = global::QLBH.SaleProduct.Properties.Resources.add_1_icon;
            this.btnAddPNK.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAddPNK.Name = "btnAddPNK";
            this.btnAddPNK.Size = new System.Drawing.Size(129, 22);
            this.btnAddPNK.Text = "Thêm mới (Ctr +N)";
            this.btnAddPNK.Click += new System.EventHandler(this.btnAddPNK_Click);
            // 
            // btnSavePNK
            // 
            this.btnSavePNK.Image = global::QLBH.SaleProduct.Properties.Resources.Save_icon;
            this.btnSavePNK.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSavePNK.Name = "btnSavePNK";
            this.btnSavePNK.Size = new System.Drawing.Size(164, 22);
            this.btnSavePNK.Text = "Lưu và Thêm mới (Ctr +S)";
            this.btnSavePNK.Click += new System.EventHandler(this.btnSavePNK_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnPrintPreview
            // 
            this.btnPrintPreview.Image = global::QLBH.SaleProduct.Properties.Resources.print_preview;
            this.btnPrintPreview.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnPrintPreview.Name = "btnPrintPreview";
            this.btnPrintPreview.Size = new System.Drawing.Size(114, 22);
            this.btnPrintPreview.Text = "Xem trước khi in";
            this.btnPrintPreview.Click += new System.EventHandler(this.btnPrintPreview_Click);
            // 
            // btnIn
            // 
            this.btnIn.Image = global::QLBH.SaleProduct.Properties.Resources.printer_icon16;
            this.btnIn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnIn.Name = "btnIn";
            this.btnIn.Size = new System.Drawing.Size(79, 22);
            this.btnIn.Text = "In (Ctr+P)";
            this.btnIn.Click += new System.EventHandler(this.btnIn_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // btnFindPNK
            // 
            this.btnFindPNK.Image = global::QLBH.SaleProduct.Properties.Resources.search_icon16;
            this.btnFindPNK.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnFindPNK.Name = "btnFindPNK";
            this.btnFindPNK.Size = new System.Drawing.Size(118, 22);
            this.btnFindPNK.Text = "Tìm kiếm (Ctr+F)";
            this.btnFindPNK.Click += new System.EventHandler(this.btnFindPNK_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.tbxChietKhau);
            this.groupBox1.Controls.Add(this.tbxThue);
            this.groupBox1.Controls.Add(this.lbNgayTao);
            this.groupBox1.Controls.Add(this.lbNguoiTao);
            this.groupBox1.Controls.Add(this.lbAddKho);
            this.groupBox1.Controls.Add(this.lbTongTienThanhToan);
            this.groupBox1.Controls.Add(this.lbTongTienHang);
            this.groupBox1.Controls.Add(this.lbSoPhieu);
            this.groupBox1.Controls.Add(this.cmbTenKho);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.label13);
            this.groupBox1.Controls.Add(this.label11);
            this.groupBox1.Controls.Add(this.label9);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.groupBox1.Location = new System.Drawing.Point(3, 28);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(1, 94);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Thông tin phiếu nhập";
            // 
            // tbxChietKhau
            // 
            this.tbxChietKhau.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxChietKhau.ForeColor = System.Drawing.Color.Red;
            this.tbxChietKhau.Location = new System.Drawing.Point(1017, 30);
            this.tbxChietKhau.Name = "tbxChietKhau";
            this.tbxChietKhau.Size = new System.Drawing.Size(158, 22);
            this.tbxChietKhau.TabIndex = 15;
            this.tbxChietKhau.Text = "0";
            this.tbxChietKhau.TextChanged += new System.EventHandler(this.tbxChietKhau_TextChanged);
            // 
            // tbxThue
            // 
            this.tbxThue.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxThue.ForeColor = System.Drawing.Color.Red;
            this.tbxThue.Location = new System.Drawing.Point(740, 66);
            this.tbxThue.Name = "tbxThue";
            this.tbxThue.Size = new System.Drawing.Size(61, 22);
            this.tbxThue.TabIndex = 15;
            this.tbxThue.Text = "10";
            this.tbxThue.TextChanged += new System.EventHandler(this.tbxThue_TextChanged);
            // 
            // lbNgayTao
            // 
            this.lbNgayTao.AutoSize = true;
            this.lbNgayTao.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNgayTao.ForeColor = System.Drawing.Color.Blue;
            this.lbNgayTao.Location = new System.Drawing.Point(377, 66);
            this.lbNgayTao.Name = "lbNgayTao";
            this.lbNgayTao.Size = new System.Drawing.Size(51, 16);
            this.lbNgayTao.TabIndex = 10;
            this.lbNgayTao.Text = "label7";
            // 
            // lbNguoiTao
            // 
            this.lbNguoiTao.AutoSize = true;
            this.lbNguoiTao.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNguoiTao.ForeColor = System.Drawing.Color.Blue;
            this.lbNguoiTao.Location = new System.Drawing.Point(104, 66);
            this.lbNguoiTao.Name = "lbNguoiTao";
            this.lbNguoiTao.Size = new System.Drawing.Size(51, 16);
            this.lbNguoiTao.TabIndex = 12;
            this.lbNguoiTao.Text = "label7";
            // 
            // lbAddKho
            // 
            this.lbAddKho.Cursor = System.Windows.Forms.Cursors.Hand;
            this.lbAddKho.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbAddKho.ForeColor = System.Drawing.Color.Blue;
            this.lbAddKho.Image = ((System.Drawing.Image)(resources.GetObject("lbAddKho.Image")));
            this.lbAddKho.Location = new System.Drawing.Point(590, 31);
            this.lbAddKho.Name = "lbAddKho";
            this.lbAddKho.Size = new System.Drawing.Size(19, 21);
            this.lbAddKho.TabIndex = 11;
            this.lbAddKho.Click += new System.EventHandler(this.lbAddKho_Click);
            // 
            // lbTongTienThanhToan
            // 
            this.lbTongTienThanhToan.AutoSize = true;
            this.lbTongTienThanhToan.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbTongTienThanhToan.ForeColor = System.Drawing.Color.Red;
            this.lbTongTienThanhToan.Location = new System.Drawing.Point(1014, 66);
            this.lbTongTienThanhToan.Name = "lbTongTienThanhToan";
            this.lbTongTienThanhToan.Size = new System.Drawing.Size(16, 16);
            this.lbTongTienThanhToan.TabIndex = 14;
            this.lbTongTienThanhToan.Text = "0";
            // 
            // lbTongTienHang
            // 
            this.lbTongTienHang.AutoSize = true;
            this.lbTongTienHang.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbTongTienHang.ForeColor = System.Drawing.Color.Red;
            this.lbTongTienHang.Location = new System.Drawing.Point(737, 34);
            this.lbTongTienHang.Name = "lbTongTienHang";
            this.lbTongTienHang.Size = new System.Drawing.Size(16, 16);
            this.lbTongTienHang.TabIndex = 14;
            this.lbTongTienHang.Text = "0";
            // 
            // lbSoPhieu
            // 
            this.lbSoPhieu.AutoSize = true;
            this.lbSoPhieu.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbSoPhieu.ForeColor = System.Drawing.Color.Blue;
            this.lbSoPhieu.Location = new System.Drawing.Point(104, 31);
            this.lbSoPhieu.Name = "lbSoPhieu";
            this.lbSoPhieu.Size = new System.Drawing.Size(51, 16);
            this.lbSoPhieu.TabIndex = 14;
            this.lbSoPhieu.Text = "label7";
            // 
            // cmbTenKho
            // 
            this.cmbTenKho.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbTenKho.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.cmbTenKho.FormattingEnabled = true;
            this.cmbTenKho.Location = new System.Drawing.Point(380, 28);
            this.cmbTenKho.Name = "cmbTenKho";
            this.cmbTenKho.Size = new System.Drawing.Size(205, 24);
            this.cmbTenKho.TabIndex = 13;
            this.cmbTenKho.SelectedIndexChanged += new System.EventHandler(this.cmbTenKho_SelectedIndexChanged);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(297, 66);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(72, 16);
            this.label5.TabIndex = 8;
            this.label5.Text = "Ngày Tạo:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(20, 66);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(69, 16);
            this.label3.TabIndex = 9;
            this.label3.Text = "Người tạo:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(297, 34);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(84, 16);
            this.label2.TabIndex = 7;
            this.label2.Text = "Tên Kho (F4)";
            // 
            // label13
            // 
            this.label13.AutoSize = true;
            this.label13.Location = new System.Drawing.Point(881, 66);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(127, 16);
            this.label13.TabIndex = 6;
            this.label13.Text = "Tổng tiền TT (VNĐ):";
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(881, 36);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(112, 16);
            this.label11.TabIndex = 6;
            this.label11.Text = "Chiết khấu (VNĐ):";
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(631, 66);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(65, 16);
            this.label9.TabIndex = 6;
            this.label9.Text = "Thuế (%):";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(631, 34);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(100, 16);
            this.label4.TabIndex = 6;
            this.label4.Text = "Tổng tiền hàng:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(20, 31);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 16);
            this.label1.TabIndex = 6;
            this.label1.Text = "Số phiếu:";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.dataGridView1);
            this.groupBox2.Controls.Add(this.radioButton1);
            this.groupBox2.Controls.Add(this.tbxSoLuong);
            this.groupBox2.Controls.Add(this.btnDelete);
            this.groupBox2.Controls.Add(this.btnAdd);
            this.groupBox2.Controls.Add(this.tbxMatHangSeach);
            this.groupBox2.Controls.Add(this.label8);
            this.groupBox2.Controls.Add(this.label7);
            this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.groupBox2.Location = new System.Drawing.Point(3, 128);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(1, 1);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Danh sách mặt hàng";
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToOrderColumns = true;
            this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridView1.BackgroundColor = System.Drawing.Color.Gray;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.dataGridViewTextBoxColumn1,
            this.dataGridViewTextBoxColumn2,
            this.dataGridViewTextBoxColumn3,
            this.dataGridViewTextBoxColumn4,
            this.dataGridViewTextBoxColumn5,
            this.dataGridViewTextBoxColumn6,
            this.dataGridViewTextBoxColumn7,
            this.dataGridViewTextBoxColumn8,
            this.dataGridViewTextBoxColumn9,
            this.dataGridViewTextBoxColumn10});
            this.dataGridView1.DataSource = this.nKMatHangModelBindingSource;
            this.dataGridView1.Location = new System.Drawing.Point(6, 85);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(0, 0);
            this.dataGridView1.TabIndex = 12;
            this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
            this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
            // 
            // dataGridViewTextBoxColumn1
            // 
            this.dataGridViewTextBoxColumn1.DataPropertyName = "PNKID";
            this.dataGridViewTextBoxColumn1.HeaderText = "PNKID";
            this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
            this.dataGridViewTextBoxColumn1.ReadOnly = true;
            this.dataGridViewTextBoxColumn1.Visible = false;
            this.dataGridViewTextBoxColumn1.Width = 73;
            // 
            // dataGridViewTextBoxColumn2
            // 
            this.dataGridViewTextBoxColumn2.DataPropertyName = "MHID";
            this.dataGridViewTextBoxColumn2.HeaderText = "MHID";
            this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
            this.dataGridViewTextBoxColumn2.ReadOnly = true;
            this.dataGridViewTextBoxColumn2.Visible = false;
            this.dataGridViewTextBoxColumn2.Width = 67;
            // 
            // dataGridViewTextBoxColumn3
            // 
            this.dataGridViewTextBoxColumn3.DataPropertyName = "MHCODE";
            this.dataGridViewTextBoxColumn3.HeaderText = "Mã MH";
            this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
            this.dataGridViewTextBoxColumn3.ReadOnly = true;
            this.dataGridViewTextBoxColumn3.Width = 76;
            // 
            // dataGridViewTextBoxColumn4
            // 
            this.dataGridViewTextBoxColumn4.DataPropertyName = "TENMH";
            this.dataGridViewTextBoxColumn4.HeaderText = "Tên MH";
            this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
            this.dataGridViewTextBoxColumn4.ReadOnly = true;
            this.dataGridViewTextBoxColumn4.Width = 81;
            // 
            // dataGridViewTextBoxColumn5
            // 
            this.dataGridViewTextBoxColumn5.DataPropertyName = "DVT";
            this.dataGridViewTextBoxColumn5.HeaderText = "DVT";
            this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5";
            this.dataGridViewTextBoxColumn5.ReadOnly = true;
            this.dataGridViewTextBoxColumn5.Width = 61;
            // 
            // dataGridViewTextBoxColumn6
            // 
            this.dataGridViewTextBoxColumn6.DataPropertyName = "SOLUONG";
            this.dataGridViewTextBoxColumn6.HeaderText = "Số lượng";
            this.dataGridViewTextBoxColumn6.Name = "dataGridViewTextBoxColumn6";
            this.dataGridViewTextBoxColumn6.ReadOnly = true;
            this.dataGridViewTextBoxColumn6.Width = 86;
            // 
            // dataGridViewTextBoxColumn7
            // 
            this.dataGridViewTextBoxColumn7.DataPropertyName = "DONGIA";
            this.dataGridViewTextBoxColumn7.HeaderText = "Đơn giá";
            this.dataGridViewTextBoxColumn7.Name = "dataGridViewTextBoxColumn7";
            this.dataGridViewTextBoxColumn7.ReadOnly = true;
            this.dataGridViewTextBoxColumn7.Width = 79;
            // 
            // dataGridViewTextBoxColumn8
            // 
            this.dataGridViewTextBoxColumn8.DataPropertyName = "CHIETKHAU";
            this.dataGridViewTextBoxColumn8.HeaderText = "Chiết khấu";
            this.dataGridViewTextBoxColumn8.Name = "dataGridViewTextBoxColumn8";
            this.dataGridViewTextBoxColumn8.ReadOnly = true;
            this.dataGridViewTextBoxColumn8.Width = 95;
            // 
            // dataGridViewTextBoxColumn9
            // 
            this.dataGridViewTextBoxColumn9.DataPropertyName = "THANHTIEN";
            this.dataGridViewTextBoxColumn9.HeaderText = "Thành tiền";
            this.dataGridViewTextBoxColumn9.Name = "dataGridViewTextBoxColumn9";
            this.dataGridViewTextBoxColumn9.ReadOnly = true;
            this.dataGridViewTextBoxColumn9.Width = 95;
            // 
            // dataGridViewTextBoxColumn10
            // 
            this.dataGridViewTextBoxColumn10.DataPropertyName = "GHICHU";
            this.dataGridViewTextBoxColumn10.HeaderText = "Ghi chú";
            this.dataGridViewTextBoxColumn10.Name = "dataGridViewTextBoxColumn10";
            this.dataGridViewTextBoxColumn10.ReadOnly = true;
            this.dataGridViewTextBoxColumn10.Visible = false;
            this.dataGridViewTextBoxColumn10.Width = 77;
            // 
            // nKMatHangModelBindingSource
            // 
            this.nKMatHangModelBindingSource.DataSource = typeof(QLBH.ModelInterface.INKMatHangModel);
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(107, 24);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(112, 20);
            this.radioButton1.TabIndex = 11;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "Dùng mã vạch";
            this.radioButton1.UseVisualStyleBackColor = true;
            // 
            // tbxSoLuong
            // 
            this.tbxSoLuong.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxSoLuong.ForeColor = System.Drawing.Color.Gray;
            this.tbxSoLuong.Location = new System.Drawing.Point(603, 49);
            this.tbxSoLuong.Name = "tbxSoLuong";
            this.tbxSoLuong.Size = new System.Drawing.Size(68, 29);
            this.tbxSoLuong.TabIndex = 8;
            // 
            // btnDelete
            // 
            this.btnDelete.Enabled = false;
            this.btnDelete.Image = ((System.Drawing.Image)(resources.GetObject("btnDelete.Image")));
            this.btnDelete.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnDelete.Location = new System.Drawing.Point(803, 49);
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(99, 29);
            this.btnDelete.TabIndex = 10;
            this.btnDelete.Text = "Xóa (Del)";
            this.btnDelete.UseVisualStyleBackColor = true;
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // btnAdd
            // 
            this.btnAdd.Image = ((System.Drawing.Image)(resources.GetObject("btnAdd.Image")));
            this.btnAdd.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnAdd.Location = new System.Drawing.Point(681, 50);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(120, 29);
            this.btnAdd.TabIndex = 9;
            this.btnAdd.Text = "Thêm (Enter)";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // tbxMatHangSeach
            // 
            this.tbxMatHangSeach.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbxMatHangSeach.ForeColor = System.Drawing.Color.Gray;
            this.tbxMatHangSeach.Location = new System.Drawing.Point(107, 50);
            this.tbxMatHangSeach.Name = "tbxMatHangSeach";
            this.tbxMatHangSeach.PopupControl = null;
            this.tbxMatHangSeach.Size = new System.Drawing.Size(406, 29);
            this.tbxMatHangSeach.TabIndex = 7;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(519, 58);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(87, 16);
            this.label8.TabIndex = 5;
            this.label8.Text = "Số lượng (F2)";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(8, 58);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(92, 16);
            this.label7.TabIndex = 6;
            this.label7.Text = "Mặt hàng (F1):";
            // 
            // PhieuNhapHangView
            // 
            this.BackColor = System.Drawing.SystemColors.Control;
            this.Controls.Add(this.tableLayoutPanel1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(42)))), ((int)(((byte)(139)))));
            this.Name = "PhieuNhapHangView";
            this.Size = new System.Drawing.Size(0, 0);
            this.Load += new System.EventHandler(this.PhieuNhapHangView_Load);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.nKMatHangModelBindingSource)).EndInit();
            this.ResumeLayout(false);

        }

        private void PhieuNhapHangView_Load(object sender, EventArgs e)
        {
            InitDatatoControl();

            listViewPopup = new ListView();

            listViewPopup.View = View.Details;
            listViewPopup.Width = 500;


            listViewPopup.Height = 300;

            listViewPopup.Columns.Add("Mã MH", 150, HorizontalAlignment.Left);
            listViewPopup.Columns.Add("Tên MH", 350, HorizontalAlignment.Left);

            listViewPopup.MultiSelect = false;
            listViewPopup.GridLines = true;
            listViewPopup.FullRowSelect = true;

            // dataGridViewEx1.PopupControl = listViewPopup;

            //dataGridViewEx1.FilterContent += dataGridViewEx1_FilterContent;
            listViewPopup.SelectedIndexChanged += new EventHandler(listViewPopup_SelectedIndexChanged);

            mathangs = MatHangController.GetInstance().GetAll().ToList();            

            tbxMatHangSeach.PopupControl = listViewPopup;
            tbxMatHangSeach.DataFilterEvent += new EventHandler(tbxMatHangSeach_DataFilterEvent);

            tbxSoLuong.MouseClick += new MouseEventHandler(tbxSoLuong_MouseClick);
        }

        void tbxSoLuong_MouseClick(object sender, MouseEventArgs e)
        {
            tbxSoLuong.SelectAll();
        }

        void tbxMatHangSeach_DataFilterEvent(object sender, EventArgs e)
        {
            TextBox editTbx = (TextBox)sender;

            var items = mathangs.Where(mh => mh.MHTEN.Contains(editTbx.Text) || mh.MHCODE.Contains(editTbx.Text)).ToList();

            BuildListViewPopupItems(items);
        }
        private void BuildListViewPopupItems(List<MATHANG> mathangs)
        {
            listViewPopup.Items.Clear();
            foreach (MATHANG mh in mathangs)
            {
                ListViewItem item = new ListViewItem();
                item.Text = mh.MHCODE;
                item.SubItems.Add(mh.MHTEN);

                listViewPopup.Items.Add(item);
            }
        }

        void listViewPopup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewPopup.SelectedItems.Count > 0)
            {
                String mhID = listViewPopup.SelectedItems[0].SubItems[0].Text;
                MATHANG mh = MatHangController.GetInstance().FindBy(h => h.MHCODE == mhID).FirstOrDefault();

                if (mh != null)
                {
                    selected = mh;
                    tbxMatHangSeach.Text = selected.MHTEN;
                    tbxSoLuong.Text = "1";
                }
                listViewPopup.Visible = false;
                tbxSoLuong.SelectAll();
                tbxSoLuong.Focus();                
            }               
        }
        private void SetDataForGrid()
        {
            nKMatHangModelBindingSource.DataSource = new BindingList<INKMatHangModel>(model.MatHang);

            dataGridView1.DataSource = nKMatHangModelBindingSource;

            decimal totalMoney = 0;
            foreach (INKMatHangModel item in model.MatHang)
            {
                totalMoney += item.THANHTIEN;
            }
            lbTongTienHang.Text = totalMoney.ToString();
            CalcTongTien();
        }
        private void InitDatatoControl()
        {
            if (dlgType == eDlgType.ADD)
            {
                model.PNKID = Guid.NewGuid();
                model.SOPHIEU = CodeHelper.GetNewCode();
                model.NGAYTAO = DateTime.Now;
                model.NGUOITAOID = AccountHandler.GetCurrentUserLogin().NVID;
                model.NGUOITAO = AccountHandler.GetCurrentUserLogin().TENNV;
            }

            lbSoPhieu.Text = model.SOPHIEU;
            lbNgayTao.Text = model.NGAYTAO.ToShortDateString();
            lbNguoiTao.Text = model.NGUOITAO;

            List<KHO> khos = KhoController.GetInstance().GetAll().ToList();

            khoHang = null;
            cmbTenKho.DataSource = khos;

            cmbTenKho.DisplayMember = "TENKHO";

            if (dlgType == eDlgType.ADD)
            {
                KHO defaultKho = khos.Where(k => k.MACDINH == true).FirstOrDefault();
                if (defaultKho != null)
                {
                    cmbTenKho.SelectedItem = defaultKho;
                }

                if (cmbTenKho.SelectedItem != null)
                {
                    khoHang = cmbTenKho.SelectedItem as KHO;

                    if (khoHang != null)
                    {
                        model.TENKHO = khoHang.TENKHO;
                        model.KHOID = khoHang.KHOID;
                    }
                }
            }
            else
            {
                khoHang = khos.Find(kh => kh.KHOID == model.KHOID);
                if (khoHang != null)
                {
                    cmbTenKho.SelectedItem = khoHang;

                    if (khoHang != null)
                    {
                        model.TENKHO = khoHang.TENKHO;
                        model.KHOID = khoHang.KHOID;
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckIntValue(tbxSoLuong.Text, "Số lượng");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxSoLuong.SelectAll();
                tbxSoLuong.Focus();
                return;
            }
            if (functionType == eDlgType.ADD)
            {
                if (selected != null)
                {
                    INKMatHangModel item = model.MatHang.Where(mh => mh.MHCODE == selected.MHCODE).FirstOrDefault();
                    if (item==null)
                    {
                        item=ModelFactory.ModelFactory.GetNKMatHangModelInstance();
                        item.InitFromMatHangModel(selected);
                        model.MatHang.Add(item);
                    }                                            
                    int soluong = int.Parse(tbxSoLuong.Text);
                    item.SOLUONG += soluong;                                                            

                    SetDataForGrid();
                    tbxMatHangSeach.Focus();
                }
            }
            else
            {
                //Edit
                int soluong = int.Parse(tbxSoLuong.Text);
                editItem.SOLUONG = soluong;

                //model.MatHang.Remove(newItem);
                SetDataForGrid();
                tbxMatHangSeach.Enabled = true;
                tbxMatHangSeach.Focus();

                btnAdd.Text = "Thêm";
                btnAdd.Image = global::QLBH.SaleProduct.Properties.Resources.add_1_icon;
                functionType = eDlgType.ADD;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow obj in dataGridView1.SelectedRows)
                {

                    model.MatHang.Remove(obj.DataBoundItem as INKMatHangModel);

                }
                // model.MatHang.Remove(newItem);
                SetDataForGrid();
                editItem = null;
            }
        }

        private void tbxThue_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckDecimalData(tbxThue.Text, "Thuế");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxThue.SelectAll();
                tbxThue.Focus();
                return;
            }
            if (decimal.Parse(tbxThue.Text) < 0)
            {
                ShowMessage.Instance.ShowError("Thuế phải là số dương"); ;
                tbxThue.SelectAll();
                tbxThue.Focus();
                return;
            }
            CalcTongTien();
        }

        private void tbxChietKhau_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckDecimalData(tbxChietKhau.Text, "Chiết khấu");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxChietKhau.SelectAll();
                tbxChietKhau.Focus();
                return;
            }
            if (decimal.Parse(tbxChietKhau.Text) < 0)
            {
                ShowMessage.Instance.ShowError("Chiết khấu phải là số dương"); ;
                tbxChietKhau.SelectAll();
                tbxChietKhau.Focus();
                return;
            }

            CalcTongTien();
        }
        private void CalcTongTien()
        {
            model.THUE = float.Parse(tbxThue.Text);
            model.CHIETKHAU = decimal.Parse(tbxChietKhau.Text);
            model.TONGTIEN = (decimal.Parse(lbTongTienHang.Text) - model.CHIETKHAU) * (decimal)(1 + model.THUE / 100);
            lbTongTienThanhToan.Text = model.TONGTIEN.ToString();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                btnDelete.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
            }
        }

        private void lbAddKho_Click(object sender, EventArgs e)
        {
            AddKhoDlg addKho = new AddKhoDlg();
            addKho.DlgType = eDlgType.ADD;

            if (addKho.ShowDialog() == DialogResult.OK)
            {
                List<KHO> khos = KhoController.GetInstance().GetAll().ToList();

                cmbTenKho.DataSource = khos;

                cmbTenKho.DisplayMember = "TENKHO";

                cmbTenKho.SelectedItem = addKho.Model;
            }
        }

        private void cmbTenKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (khoHang != null && khoHang != cmbTenKho.SelectedItem as KHO && model.MatHang.Count > 0)
            {
                DialogResult result = MessageBox.Show("Phiếu nhập hàng bạn đang lập cho kho cũ sẽ bị xóa, bạn có muốn tiếp tục không?",
                "Đổi kho hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    model.MatHang.Clear();
                    khoHang = cmbTenKho.SelectedItem as KHO;
                    SetDataForGrid();
                }
                else
                {
                    cmbTenKho.SelectedItem = khoHang;
                }
            }
            if (khoHang!=null)
            {
                model.TENKHO = khoHang.TENKHO;
                model.KHOID = khoHang.KHOID;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                INKMatHangModel itemSelected = dataGridView1.SelectedRows[0].DataBoundItem as INKMatHangModel;

                if (itemSelected != null)
                {
                    editItem = itemSelected;
                    tbxMatHangSeach.SetText(editItem.TENMH);
                    tbxSoLuong.Text = editItem.SOLUONG.ToString();

                    btnAdd.Text = "Sửa";
                    functionType = eDlgType.EDIT;
                    this.btnAdd.Image = global::QLBH.SaleProduct.Properties.Resources.edit_icon;

                    tbxSoLuong.SelectAll();
                    tbxSoLuong.Focus();

                    tbxMatHangSeach.Enabled = false;
                }
            }
        }

        public ReportDocument BuildReportDocument()
        {
            ReportDocument doc = ReportLoader.GetReport(eReportTypes.RP_PHIEUNHAPHANG);
            if (doc != null)
            {
                //Set data source for reports
                DataSetNhapKho ds = new DataSetNhapKho();
                IPhieuNhapKhoModel phieunhapkho = model as IPhieuNhapKhoModel;
                phieunhapkho.PNKID = Guid.NewGuid();

                DataTable dt1 = DataSourceHelper.ToDataTable(new List<IPhieuNhapKhoModel>() { phieunhapkho });
                DataTable dt2 = DataSourceHelper.ToDataTable(model.MatHang);
                ds.PhieuNhapKho.Merge(dt1);
                ds.NKChiTiet.Merge(dt2);
                doc.SetDataSource(ds);
                doc.SetParameterValue("CompanyName", "Công ty TNHH MED-AID");
                doc.SetParameterValue("Address", "Lô 12, BT1, X2, Bắc linh đàm, Hoàng Mai, Hà Nội");
            }
            else
            {
                ShowMessage.Instance.ShowError("Không tìm thấy báo cáo!");
            }
            return doc;
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            ReportDocument doc = BuildReportDocument();
            ReportDialogBase report = new ReportDialogBase(doc);
            report.ShowDialog();
            this.Cursor = Cursors.Default;
        }
        
        private void btnIn_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            ReportDocument doc = BuildReportDocument();
            ShowPrintDlg(doc);
            this.Cursor = Cursors.Default;
        }
        private void ShowPrintDlg(ReportDocument rp)
        {
            PrintDialog printDlg = new PrintDialog();
            printDlg.AllowSomePages = true;

            if (printDlg.ShowDialog(this) == DialogResult.OK)
            {
                int nCopies = printDlg.PrinterSettings.Copies;
                int startPage = printDlg.PrinterSettings.FromPage;
                int toPage = printDlg.PrinterSettings.ToPage;
                bool collated = printDlg.PrinterSettings.Collate;

                System.Drawing.Printing.PrinterSettings printerSetting = printDlg.PrinterSettings;
                try
                {
                    rp.PrintOptions.PrinterName = printerSetting.PrinterName;
                    rp.PrintToPrinter(nCopies, collated, startPage, toPage);
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                }
            }
        }

        private void btnAddPNK_Click(object sender, EventArgs e)
        {
            dlgType = eDlgType.ADD;
            model = null;
            model = ModelFactory.ModelFactory.GetPhieuNhapKhoCTModelInstance();

            InitDatatoControl();

            SetDataForGrid();
        }
        private void btnSavePNK_Click(object sender, EventArgs e)
        {
            try
            {
                PHIEUNHAPKHO entity = model.ToEntity();

                if (dlgType == eDlgType.ADD)
                {
                    PhieuNhapKhoController.GetInstance().Add(entity);
                }
                else
                {
                    PhieuNhapKhoController.GetInstance().Edit(entity);
                }

                //Update mathang to kho
                UpdateMatHangtoKho();

                btnAddPNK_Click(btnAddPNK, null);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }        
        }

        private void UpdateMatHangtoKho()
        {
            try
            {
                if (dlgType == eDlgType.ADD)
                {
                    //add
                    foreach (INKMatHangModel mh in model.MatHang)
                    {
                        IKho_MatHangModel kho_mh = ModelFactory.ModelFactory.GetKho_MatHangModelInstance();
                        kho_mh.InitFromNKMatHangModel(mh);
                        kho_mh.KHOID = model.KHOID;
                        Kho_MathangController.GetInstance().Add(kho_mh.ToEntity());
                    }
                }
                else
                {
                    //edit                    
                    foreach (INKMatHangModel mh in model.MatHang)
                    {
                        IKho_MatHangModel kho_mh = ModelFactory.ModelFactory.GetKho_MatHangModelInstance();
                        kho_mh.InitFromNKMatHangModel(mh);
                        kho_mh.KHOID = model.KHOID;
                        Kho_MathangController.GetInstance().Edit(kho_mh.ToEntity());
                    }
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }            
        }

        private void btnFindPNK_Click(object sender, EventArgs e)
        {
            FindPNKDlg findPNKDlg = new FindPNKDlg();
            if (findPNKDlg.ShowDialog()==DialogResult.OK)
            {
                IPhieuNhapKhoModel pnkmodel = findPNKDlg.Model;
                model.InitFromEntity(pnkmodel.ToEntity());

                dlgType = eDlgType.EDIT;

                InitDatatoControl();

                SetDataForGrid();
            }
        }
        //hot key define to run function on form
        public override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Control && e.KeyCode==Keys.N)
            {
                btnAddPNK_Click(btnAddPNK, null);
            }
            else if (e.Control && e.KeyCode==Keys.S)
            {
                btnSavePNK_Click(btnSavePNK, null);
            }
            else if (e.Control && e.KeyCode==Keys.P)
            {
                btnIn_Click(btnIn, null);
            } 
            else if(e.Control && e.KeyCode==Keys.F)
            {
                btnFindPNK_Click(btnFindPNK, null);
            }
            else if (e.KeyCode==Keys.F1)
            {
                tbxMatHangSeach.SelectAll();
                tbxMatHangSeach.Focus();
            }
            else if (e.KeyCode == Keys.F2)
            {
                tbxSoLuong.SelectAll();
                tbxSoLuong.Focus();
            }
            else if (e.KeyCode == Keys.Enter)
            {                
                btnAdd_Click(btnAdd, null);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                if (btnDelete.Enabled == true)
                {
                    btnDelete_Click(btnDelete, null);
                }
            }
            base.OnKeyDown(e);
        }
    }
}
